<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Exception;
use Illuminate\Support\Facades\Session;
use TCPDF;
use PhpOffice\PhpWord\PhpWord;

class CustomerController extends Controller{

          public function list(){

            if(Session::get('role_id') == 1 ) {

                $customer = DB::table('customer')
                ->select('id','customer_name','telephone','address','type',
                    'contact','position','contact_telephone','contact2','contact_telephone2','position2','balance')
                    ->where('status',0)
                    ->get();

            }else if(Session::get('role_id') == 2 ){

                  $servicer = DB::table('servicer')
                       ->select('id')
                       ->where('company_id',Session::get('session_id'))
                        ->get();
                   $servicer_id = [];
                   foreach ($servicer as $key=>$value){
                       $servicer_id[] = $value->id;
                   }
                $customer = DB::table('customer')
                   ->select('id','customer_name','telephone','address','type',
                    'contact','position','contact_telephone','contact2','contact_telephone2','position2','balance')
                    ->where('status',0)
                    ->whereIn('servicer_id',$servicer_id)
                    ->get();
             }else if(Session::get('role_id') == 3 ){

                  $servicer = DB::table('servicer')
                       ->select('id')
                       ->where('company_id',Session::get('company_id'))
                        ->get();
                   $servicer_id = [];
                   foreach ($servicer as $key=>$value){
                       $servicer_id[] = $value->id;
                   }
                $customer = DB::table('customer')
                   ->select('id','customer_name','telephone','address','type',
                    'contact','position','contact_telephone','contact2','contact_telephone2','position2','balance')
                    ->where('status',0)
                    ->whereIn('servicer_id',$servicer_id)
                    ->get();
             }
                $customer = json_decode($customer,1);
                return view('admin.customer.list', ['customer' =>$customer]);

       }


       /*
        *客户删除
        * */

        public function customer_delete(Request $request){

            $bool = DB::table('customer')->where('id',$request->id)->update(array('status'=>1));
                if($bool){
                    echo "<script language='javascript'>alert('删除成功');window.location.href='/admin/customer/list';</script>";
                }else{
                   echo "<script language='javascript'>alert('删除失败');window.location.href='/admin/customer/list';</script>";
             }

        }

        /*
         * 查看合同
         * */

        public function agreement_edite(){

            if(Session::get('role_id') == 1 ) {
                $agreement = DB::table('agreement')
                    ->select('id','agreement_method','agreement_code','agreement_date','agreement_order','agreement_position',
                        'supplyer_company','customer_name','take_name','total_sell','take_phone','create_date','site_address','get_address',
                        'get_company','sign_agreement_id','get_position'
                    )
                    ->where('status',1)
                    ->get();

            }else if(Session::get('role_id') == 2 ){

                $servicer = DB::table('servicer')
                       ->select('id')
                       ->where('company_id',Session::get('session_id'))
                        ->get();
                   $servicer_id = [];
                   foreach ($servicer as $key=>$value){
                       $servicer_id[] = $value->id;
                   }

                $agreement = DB::table('agreement')
                    ->select('id','agreement_method','agreement_code','agreement_date','agreement_order','agreement_position',
                        'supplyer_company','customer_name','take_name','total_sell','take_phone','create_date','site_address','get_address',
                        'get_company','sign_agreement_id','get_position'
                    )
                    ->where('status',1)
                    ->whereIn('servicer_id',$servicer_id)->get();
            }else if(Session::get('role_id') == 3 ){

                $servicer = DB::table('servicer')
                       ->select('id')
                       ->where('company_id',Session::get('company_id'))
                        ->get();
                   $servicer_id = [];
                   foreach ($servicer as $key=>$value){
                       $servicer_id[] = $value->id;
                   }

                $agreement = DB::table('agreement')
                    ->select('id','agreement_method','agreement_code','agreement_date','agreement_order','agreement_position',
                        'supplyer_company','customer_name','take_name','total_sell','take_phone','create_date','site_address','get_address',
                        'get_company','sign_agreement_id','get_position'
                    )
                    ->where('status',1)
                    ->whereIn('servicer_id',$servicer_id)->get();
            }

            return view('admin.customer.agreement_edite',['agreement'=>$agreement]);

          }



          /*
           * 合同下载
           * */
          public function agreement_download(Request $request){


                $agreement = DB::table('agreement')
                   ->select('status','sign_agreement_id','order_product_id')
                   ->where('id',$request->agreement_id)
                   ->first();

               if ($agreement->status == 0){
                   echo "<script language='javascript'>alert('下载失败');window.location.href='/admin/customer/agreement_edite';</script>";
                   exit;
               }else if($agreement->status == 1){

                        $info = DB::table('sign_agreement')
                            ->where('id',$agreement->sign_agreement_id)->first();
                        $order_product_id = explode(',',$agreement->order_product_id);
                        $info = (array)$info;
                        if(!empty($info)) {
                            $product = DB::table('order_product')->whereIn('id', $order_product_id)->get();
                            $info['product'] = $product;
                            $info = (object)$info;
                            $view  = view('admin.order.agreement_pdf', ['info' => $info])->__toString();
                            //return view('admin.order.agreement_pdf', ['info' => $info]);
                            $this->export_pdf($view);
                        }else{
                           echo "<script language='javascript'>alert('下载失败');window.location.href='/admin/customer/agreement_edite';</script>";
                        }

               }

          }



                      //导出Pdf
        public function export_pdf($view = ''){
                //$view = $this->agreement_download(2);

                $pdf = new TCPDF("P", "mm", array(300, 450));
                // 设置文档信息
                $pdf->SetCreator('');
                $pdf->SetAuthor('');
                $pdf->SetTitle('TCPDF示例');
                $pdf->SetSubject('TCPDF示例');
                $pdf->SetKeywords('TCPDF, PDF, PHP');

                // 设置页眉和页脚信息
                $pdf->SetHeaderData('tcpdf_logo.jpg', 30, '得佐', '', [0, 64, 255], [0, 64, 128]);
                $pdf->setFooterData([0, 64, 0], [0, 64, 128]);

                // 设置页眉和页脚字体
                $pdf->setHeaderFont(['stsongstdlight', '', '10']);
                $pdf->setFooterFont(['helvetica', '', '8']);

                // 设置默认等宽字体
                $pdf->SetDefaultMonospacedFont('courier');

                // 设置间距
                $pdf->SetMargins(15, 15, 15);//页面间隔
                $pdf->SetHeaderMargin(5);//页眉top间隔
                $pdf->SetFooterMargin(10);//页脚bottom间隔

                // 设置分页
                $pdf->SetAutoPageBreak(true, 25);

                // set default font subsetting mode
                $pdf->setFontSubsetting(true);

                //设置字体 stsongstdlight支持中文
                $pdf->SetFont('stsongstdlight', '', 12);

                //第一页
                $pdf->AddPage();
                //$html = '<p style="color: red">我是第二342342343行内容</p>';
                $html = $view;
                $pdf->writeHTML($html);
                $pdf->Ln(5);//换行符
                //输出PDF
                return $pdf->Output(date('Y-m-d') . '.pdf', 'D');//I输出、D下载
            }









          /*
           * 合同编辑
           * */
          public function agreement_edite_do(Request $request){

               $agreement = DB::table('agreement')
                   ->select('status','sign_agreement_id','order_product_id')
                   ->where('id',$request->agreement_id)
                   ->first();

               if ($agreement->status == 0){
                   echo '<center><h5>业务员还没签订合同，无法查看</h5></center>';
                   exit;
               }else if($agreement->status == 1){
                  try{
                        $info = DB::table('sign_agreement')
                            ->where('id',$agreement->sign_agreement_id)->first();
                        $order_product_id = explode(',',$agreement->order_product_id);
                        $info = (array)$info;
                        if(!empty($info)) {
                            $product = DB::table('order_product')->whereIn('id', $order_product_id)->get();
                            $info['product'] = $product;
                            $info = (object)$info;

                            return view('admin.customer.agreement', ['info' => $info]);
                        }else{
                            echo '<center><h5>合同为空，无法查看</h5></center>';
                        }
                   }catch (\Exception $e){
                    echo '<center><h5>合同有错误，无法查看</h5></center>';
                   }
               }

          }


          /*
           * 合同编辑
           * */
          public function agreement_edite_process(Request $request){

                    $list = $request->all();
                    $id = $list['id'];
                    unset($list['_token']);
                    unset($list['id']);
                    foreach ($list as $key=>$value){
                        if(empty($value)){
                            echo '<script language="javascript">alert("数据有空");window.location.href="/admin/customer/agreement_edite_do?agreement_id="'.$id.';</script>"';
                            exit;
                        }
                    }

                    try{
                        DB::table('sign_agreement')->where('id',$id)->update($list);
                        echo '<script language="javascript">alert("修改成功");window.location.href="/admin/customer/agreement_edite";</script>"';
                    }catch (Exception $e){
                        echo '<script language="javascript">alert("修改失败");window.location.href="/admin/customer/agreement_edite";</script>"';
                    }


          }




          public function agreement_list(Request $request){

               if(Session::get('role_id') == 1 ) {
                $agreement = DB::table('agreement')
                    ->select('id','agreement_method','agreement_code','agreement_date','agreement_order','agreement_position',
                        'supplyer_company','customer_name','take_name','total_sell','take_phone','create_date','site_address','get_address',
                        'get_company','sign_agreement_id','get_position'
                    )
                    ->where('status',1)
                    ->get();

            }else if(Session::get('role_id') == 2 ){

                $servicer = DB::table('servicer')
                       ->select('id')
                       ->where('company_id',Session::get('session_id'))
                        ->get();
                   $servicer_id = [];
                   foreach ($servicer as $key=>$value){
                       $servicer_id[] = $value->id;
                   }

                $agreement = DB::table('agreement')
                    ->select('id','agreement_method','agreement_code','agreement_date','agreement_order','agreement_position',
                        'supplyer_company','customer_name','take_name','total_sell','take_phone','create_date','site_address','get_address',
                        'get_company','sign_agreement_id','get_position'
                    )
                    ->where('status',1)
                    ->whereIn('servicer_id',$servicer_id)->get();
            }else if(Session::get('role_id') == 3 ){

                $servicer = DB::table('servicer')
                       ->select('id')
                       ->where('company_id',Session::get('company_id'))
                        ->get();
                   $servicer_id = [];
                   foreach ($servicer as $key=>$value){
                       $servicer_id[] = $value->id;
                   }

                $agreement = DB::table('agreement')
                    ->select('id','agreement_method','agreement_code','agreement_date','agreement_order','agreement_position',
                        'supplyer_company','customer_name','take_name','total_sell','take_phone','create_date','site_address','get_address',
                        'get_company','sign_agreement_id','get_position'
                    )
                    ->where('status',1)
                    ->whereIn('servicer_id',$servicer_id)->get();
            }

               return view('admin.customer.agreement_list',['agreement'=>$agreement]);

          }









}